#!/bin/bash

# 定义颜色
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 获取脚本所在目录的绝对路径
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# 计算packet目录的路径 (相对于脚本位置的上级目录的packet子目录)
PACKET_DIR="$(realpath "${SCRIPT_DIR}/../packet")"

echo -e "${BLUE}正在更新系统包列表...${NC}"
# sudo apt-get update
# sudo apt-get upgrade
echo -e "${GREEN}系统包列表更新完成${NC}"

echo -e "${BLUE}正在安装必要的系统软件包...${NC}"
sudo apt-get install -y \
make \
gcc \
g++ \
gdb \
gdbserver \
gcc-multilib \
g++-multilib \
gcc-arm-none-eabi \
qemu-system-arm \
\
gcc-arm-none-eabi \
gdb-multiarch \
libusb-0* \
libusb-1* \
openocd \
libreadline5

echo -e "${GREEN}系统软件包安装完成${NC}"

# 检查并创建packet目录（如果不存在）
if [ ! -d "$PACKET_DIR" ]; then
	echo -e "${YELLOW}packet目录不存在，创建目录: ${PACKET_DIR}${NC}"
	mkdir -p "$PACKET_DIR"
fi

echo -e "${BLUE}检查本地程序包...${NC}"

# 检查本地程序包并安装
install_local_package() {
	local package_file=$1
	local package_name=$(basename "$package_file")
	
	if [ -f "$package_file" ]; then
		echo -e "${YELLOW}发现本地程序包: $package_name${NC}"
		
		# 根据文件类型执行不同的安装命令
		if [[ "$package_file" == *.deb ]]; then
			echo -e "${BLUE}正在安装Debian包: $package_name${NC}"
			sudo dpkg -i "$package_file"
			# 修复可能的依赖问题
			sudo apt-get install -f -y
		elif [[ "$package_file" == *.tar.gz ]] || [[ "$package_file" == *.tgz ]]; then
			echo -e "${BLUE}正在解压并安装tar包: $package_name${NC}"
			local extract_dir="/tmp/extract_$(date +%s)"
			mkdir -p "$extract_dir"
			tar -xzf "$package_file" -C "$extract_dir"
			
			# 检查是否有install脚本
			if [ -f "$extract_dir/install.sh" ]; then
				echo -e "${BLUE}执行安装脚本...${NC}"
				chmod +x "$extract_dir/install.sh"
				cd "$extract_dir" && ./install.sh
			else
				echo -e "${YELLOW}未找到安装脚本，尝试直接复制可执行文件到/usr/local/bin${NC}"
				# 查找所有可执行文件并复制到/usr/local/bin
				find "$extract_dir" -type f -executable -exec sudo cp {} /usr/local/bin/ \;
			fi
			
			# 清理临时目录
			rm -rf "$extract_dir"
		elif [[ "$package_file" == *.run ]] || [[ "$package_file" == *.sh ]]; then
			echo -e "${BLUE}正在执行可执行安装脚本: $package_name${NC}"
			chmod +x "$package_file"
			sudo "$package_file"
		elif [[ "$package_file" == *.zip ]]; then
			echo -e "${BLUE}正在解压并安装zip包: $package_name${NC}"
			local extract_dir="/tmp/extract_$(date +%s)"
			mkdir -p "$extract_dir"
			unzip "$package_file" -d "$extract_dir"
			
			# 检查是否有install脚本
			if [ -f "$extract_dir/install.sh" ]; then
				echo -e "${BLUE}执行安装脚本...${NC}"
				chmod +x "$extract_dir/install.sh"
				cd "$extract_dir" && ./install.sh
			else
				echo -e "${YELLOW}未找到安装脚本，尝试直接复制可执行文件到/usr/local/bin${NC}"
				# 查找所有可执行文件并复制到/usr/local/bin
				find "$extract_dir" -type f -executable -exec sudo cp {} /usr/local/bin/ \;
			fi
			
			# 清理临时目录
			rm -rf "$extract_dir"
		else
			echo -e "${RED}未知的程序包类型: $package_name, 跳过安装${NC}"
		fi
		
		echo -e "${GREEN}本地程序包 $package_name 安装完成${NC}"
	fi
}

# 检查packet目录是否存在
if [ -d "$PACKET_DIR" ]; then
	echo -e "${BLUE}在目录 $PACKET_DIR 中查找程序包...${NC}"
	
	# 获取所有支持的包文件
	package_files=$(find "$PACKET_DIR" -maxdepth 1 -type f \( -name "*.deb" -o -name "*.tar.gz" -o -name "*.tgz" -o -name "*.run" -o -name "*.sh" -o -name "*.zip" \))
	
	# 检查是否找到程序包
	if [ -z "$package_files" ]; then
		echo -e "${YELLOW}在 $PACKET_DIR 目录中未找到可安装的程序包${NC}"
	else
		echo -e "${GREEN}找到 $(echo "$package_files" | wc -l) 个可安装的程序包${NC}"
		
		# 循环安装每个包
		while IFS= read -r package_file; do
			install_local_package "$package_file"
		done <<< "$package_files"
	fi
else
	echo -e "${YELLOW}目录 $PACKET_DIR 不存在，跳过本地程序包安装${NC}"
fi

echo -e "${GREEN}所有安装操作已完成${NC}"
